# coding:utf-8
import json
import os
import re
from PIL import Image
from PIL import ImageDraw
image_files = "F:\\project\\wanbing\\September\\image\\"
new_files = "F:\\project\\wanbing\\September\\img2\\"
def tuomin(path,pa):
	s = os.listdir(image_files)
	count = 1
	for i in s:
		document = os.path.join(image_files, i)
		img = Image.open(document)
		ii = i.split('.')
		# out = img.resize((1487, 2105))
		listStr = str(ii[0])
		fileName = ''.join(listStr)
		img.save(new_files + os.sep + '%s.jpg' % fileName)
		count = count + 1
	pt=pa.split(".")
	pa=pt[0]
	x = ''
	base_dir = "F:\\project\\wanbing\\September\\img2\\"
	new_txt=path+txt
	f = open(new_txt,'r',encoding='utf-8')
	contents = f.read()
	data = json.loads(contents)
	arr = []
	acc={}
	t = 0
	for v in data["ocr"]:
		a ="%s" % v["word"]
		b ="%s" % v["pos"]
		pattern = re.compile(r"姓名:|姓名年龄|性别|出生地|地址|现住址|身份证号|手机号|卡号|先生|女士|签名|患者：|Name|配偶|证件号码|患者签名|电话")
		result = pattern.findall(a)
		for item in result:
			if item in a :
				t+=1
				"".join(b.split())
				arr = b.split(',')
				x=arr[0][1:]
				y=arr[1][1:]
				x1=arr[6][1:]
				y1=arr[7][1:-1]
				acc[t]=[x,y,x1,y1]
	f.close()
	image=Image.open(base_dir + pa+".jpg")
	drawObject = ImageDraw.Draw(image)
	for ke, va in acc.items():
		drawObject.rectangle((int(va[0]), int(va[1]), int(va[2]), int(va[3])), fill=(0, 255, 255))
		# print(int(va[0]), int(va[1]), int(va[2]), int(va[3]))

	File_Path = os.getcwd() + '\\new_image\\'  # 获取到当前文件的目录，并检查是否有new_image文件夹，如果不存在则自动新建report文件
	if not os.path.exists(File_Path):
		os.makedirs(File_Path)
	image.save(File_Path+ pa+".jpg")


if __name__ == '__main__':
	path = "F:\\project\\wanbing\\September\\txt\\"
	txtname = os.listdir(path)
	for txt in txtname:
		tuomin(path,txt)


